#include "mainwindow.h"
#include "certmgr/certmgrwindow.h"

#include <QApplication>
#include <qcommandlineparser.h>
#include <memory.h>

#include <libgen.h>
#include <qdebug.h>

static int ShowCertMgrWindow();
static int ShowMainWindow();

static int UnitTest();

int main(int argc, char *argv[])
{
  // setenv("SSL_CERT_DIR", "/etc/ssl/certs/", 1);
  // fprintf(stderr, "LD_LIBRARY_PATH=%s\n", dirname(argv[0]));

  QApplication a(argc, argv);

  QCommandLineOption runSubCommandOption("exec",
      QCoreApplication::translate("main",
          "execute a command, select from mainwindow|certmanager|testgmssl"),
          "mainwindow");
  QCommandLineParser parser;
  parser.setApplicationDescription("A Exprimental App For GMSSL");
  parser.addHelpOption();


  // runSubCommandOption.setDefaultValues(
  //     {("mainwindow"),
  //     ("certmanager"),
  //     ("testgmssl")});
  parser.addOption(runSubCommandOption);

  QStringList arguments = qApp->arguments();
  qInfo() << arguments;

  parser.process(a);

  if(parser.isSet("exec")) {
    QString subCommand = parser.value("exec");
    if(subCommand == "mainwindow")
      return ShowMainWindow();
    else if(subCommand == "certmanager")
      return ShowCertMgrWindow();
    else if (subCommand == "testgmssl")
      return UnitTest();
  } else
    return ShowMainWindow();

  return a.exec();
}

int ShowMainWindow() {
  MainWindow w;
  w.show();
  return qApp->exec();
}

int ShowCertMgrWindow() {
  CertMgrWindow w;
  w.show();
  return qApp->exec();
}

int UnitTest() {

}
